//
//  HMCDKController.m
//  HowToDo
//
//  Created by 李贺 on 2017/1/10.
//  Copyright © 2017年 KY. All rights reserved.
//

#import "HMCDKController.h"
#import "Masonry.h"
#import "HMCDKTextField.h"
#import <Accelerate/Accelerate.h>
#import "UILabel+Addition.h"
@interface HMCDKController ()<UITextFieldDelegate>

@end

@implementation HMCDKController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"兑换码";
    UIBarButtonItem *rightBar = [[UIBarButtonItem alloc] init];
    rightBar.title = @"完成";
    self.navigationItem.rightBarButtonItem = rightBar;

    [self makeCDK];
    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return true;
}

//cdk界面
- (void)makeCDK{
    UIImageView  *imageView=[[UIImageView alloc]init];
    imageView.contentMode=UIViewContentModeScaleAspectFill;
    imageView.image=[HMCDKController boxblurImage:[UIImage imageNamed:@"timg"] withBlurNumber:0.3];
    imageView.clipsToBounds=YES;
    [self.view addSubview:imageView];
    
    imageView.userInteractionEnabled = YES;
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view.mas_top).offset(64);
        make.height.offset(240);
    }];
    imageView.contentMode = UIViewContentModeScaleToFill;
    
    UIImageView *imgView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"username"]];
    imgView.contentMode = UIViewContentModeScaleToFill;
    HMCDKTextField *textField = [[HMCDKTextField alloc] init];
    textField.leftView =imgView;
    
    textField.leftViewMode = UITextFieldViewModeAlways;
    
    textField.borderStyle = UITextBorderStyleRoundedRect;
    [imageView addSubview:textField];
    
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.mas_left).offset(10);
        make.right.equalTo(imageView.mas_right).offset(-10);
        make.top.equalTo(imageView.mas_top).offset(40);
        make.height.offset(60);
    }];
    textField.placeholder = @" 请输入用户名";
    textField.backgroundColor = [UIColor colorWithWhite:0.95 alpha:0.5];
    textField.textColor = [UIColor whiteColor];
    
    
    HMCDKTextField *CDKTextField = [[HMCDKTextField alloc] init];
    
    UIImageView *img =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cdk"]];
    img.contentMode = UIViewContentModeScaleToFill;
    CDKTextField.leftView =img;
    
    CDKTextField.leftViewMode = UITextFieldViewModeAlways;
    CDKTextField.placeholder = @" 请输入兑换码";
    CDKTextField.borderStyle = UITextBorderStyleRoundedRect;
    [imageView addSubview:CDKTextField];
    
    [CDKTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(textField.mas_left);
        make.right.equalTo(textField.mas_right);
        make.top.equalTo(textField.mas_bottom).offset(40);
        make.height.offset(60);
    }];
    
    CDKTextField.backgroundColor = [UIColor colorWithWhite:0.95 alpha:0.5];
    CDKTextField.textColor = [UIColor whiteColor];
    
    textField.delegate = self;//设置代理
    CDKTextField.delegate = self;//设置代理
    
    
    UIImageView *titleView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"hh"]];
    [self.view addSubview:titleView];
    
    UILabel*titleLable = [UILabel hm_labelWithText:@"兑换规则" fontSize:k_FONT_BIG color:[UIColor blackColor]];
    [self.view addSubview:titleLable];
    
    
    UILabel *infoLable = [UILabel hm_labelWithText:@"兑换码是由黑马知道官方制作发售,可在应用内验证使用,兑换电子书,音频和订阅的产品;你可以在微信商城或者公众号中获得该兑换码;兑换码一经售出,不支持退款,不支持更换!请在有效期内进行兑换,按要求输入要兑换到的账号;如有问题,我们也解决不了,不客气!(点击return退出键盘)" fontSize:k_FONT_NORMAL color:[UIColor darkGrayColor]];
    
    [self.view addSubview:infoLable];
    
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.mas_left).offset(k_FONT_SMALL);
        make.top.equalTo(imageView.mas_bottom).offset(k_FONT_BIG * 2);
    }];
    [titleView sizeToFit];
    
    [titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleView);
        make.left.equalTo(titleView.mas_right).offset(k_FONT_SMALL);
    }];
    
    [infoLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLable);
        make.top.equalTo(titleLable.mas_bottom).offset(k_FONT_SMALL);
        make.width.offset(self.view.bounds.size.width - k_FONT_SMALL * 6);
    }];
    infoLable.numberOfLines = 0;
}


#pragma mark -高斯模糊-
+(UIImage *)boxblurImage:(UIImage *)image withBlurNumber:(CGFloat)blur
{
    if (blur < 0.f || blur > 1.f) {
        blur = 0.5f;
    }
    int boxSize = (int)(blur * 40);
    boxSize = boxSize - (boxSize % 2) + 1;
    CGImageRef img = image.CGImage;
    vImage_Buffer inBuffer, outBuffer;
    vImage_Error error;
    void *pixelBuffer;
    //从CGImage中获取数据
    CGDataProviderRef inProvider = CGImageGetDataProvider(img);
    CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);
    //设置从CGImage获取对象的属性
    inBuffer.width = CGImageGetWidth(img);
    inBuffer.height = CGImageGetHeight(img);
    inBuffer.rowBytes = CGImageGetBytesPerRow(img);
    inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);
    pixelBuffer = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img));
    if(pixelBuffer == NULL)
        NSLog(@"No pixelbuffer");
    outBuffer.data = pixelBuffer;
    outBuffer.width = CGImageGetWidth(img);
    outBuffer.height = CGImageGetHeight(img);
    outBuffer.rowBytes = CGImageGetBytesPerRow(img);
    error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
    if (error) {
        NSLog(@"error from convolution %ld", error);
    }
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef ctx = CGBitmapContextCreate( outBuffer.data, outBuffer.width, outBuffer.height, 8, outBuffer.rowBytes, colorSpace, kCGImageAlphaNoneSkipLast);
    CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
    UIImage *returnImage = [UIImage imageWithCGImage:imageRef];
    //clean up CGContextRelease(ctx);
    CGColorSpaceRelease(colorSpace);
    free(pixelBuffer);
    CFRelease(inBitmapData);
    CGColorSpaceRelease(colorSpace);
    CGImageRelease(imageRef);
    return returnImage;
}

@end
